package com.example.myui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.myui.R;
import com.example.myui.activity.appui.AliPayActivity;
import com.example.myui.activity.appui.PersonalInfoActivity;
import com.example.myui.adapter.SelectorAdapter;
import com.example.myui.adapter.SelectorAdapterMonth;
import com.example.myui.adapter.SelectorAdapterYear;
import com.example.myui.adapter.TestAdapter;
import com.example.myui.model.YearMonthModel;
import com.example.myui.utils.DensityUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

public class UIActivity extends AppCompatActivity implements OnItemClickListener {

    private static final String TAG = UIActivity.class.getSimpleName();
    private ArrayList<String> strings;
    private TestAdapter adapter = null;
    @BindView(R.id.recyclerview_test)
    RecyclerView recyclerView;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
//        TestAdapter testAdapter = new TestAdapter(UIActivity.this, strings);
//        recyclerView.setAdapter(testAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshUI(false, refreshlayout);
                    }
                }, 2000);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshUI(true, refreshlayout);
                    }
                }, 2000);
            }
        });
    }

    private void refreshUI(boolean isLoadmoreOrRefresh, RefreshLayout refreshlayout) {
        if (isLoadmoreOrRefresh) {
            if (adapter == null) {
                adapter = new TestAdapter(UIActivity.this);
                recyclerView.setAdapter(adapter);
            }
            adapter.setNewData(initData());
            refreshlayout.finishRefresh();
            refreshlayout.setLoadmoreFinished(false);
        } else {
            if (adapter == null) return;
            adapter.addData(initData());
            refreshlayout.finishLoadmore();
        }
        Log.i(TAG, "refreshUI: " + adapter.getItemCount());
    }

    public ArrayList<String> initData() {
        strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            strings.add(i + "");
        }
        return strings;
    }

    @OnClick(R.id.btn_for_result)
    public void forResult(View view){
        startActivity(new Intent(UIActivity.this,ForResultActivity.class));
    }

    @OnClick(R.id.btn_hunpai)
    public void hunPai(View view) {
        startActivity(new Intent(UIActivity.this,HunPaiActivity.class));
    }

    @OnClick(R.id.btn_search)
    public void searchDemo(View view) {
        startActivity(new Intent(UIActivity.this, SearchActivity.class));
    }

    @OnClick(R.id.smartrefreshlayout1)
    public void smartRefreshLayou(View view) {
        startActivity(new Intent(UIActivity.this, TestRefrehActivity.class));
    }

    @OnClick(R.id.btn_md_dialog)
    public void shoMDDialog(View view) {
        startActivity(new Intent(UIActivity.this, MDDialogActivity.class));
    }

    @OnClick(R.id.btn_alerter1)
    public void alerter(View view) {
        startActivity(new Intent(UIActivity.this, AlerterActivity.class));
    }


    public void doPersionalInfo(View view) {
        startActivity(new Intent(UIActivity.this, PersonalInfoActivity.class));
    }

    public void doAliPay(View view) {
        startActivity(new Intent(UIActivity.this, AliPayActivity.class));
    }

    public void doListAndGride(View view) {
        startActivity(new Intent(UIActivity.this, ListAndGrideActivity.class));
    }

    @OnClick(R.id.btn_my_experision)
    public void myExpersion(View view) {
        startActivity(new Intent(UIActivity.this, InsertActivity.class));
    }

    @OnClick(R.id.btn_drap_menu)
    public void showDrapMenu(View view) {
//        startActivity(new Intent(UIActivity.this,DrapMenuActivity.class));
        startActivity(new Intent(UIActivity.this, DrapMenuActivity1.class));
    }

    @OnClick(R.id.btn_webview_recyclerview)
    public void showWebRecyclerview(View view) {
        startActivity(new Intent(UIActivity.this, WebRecyclerViewActivity.class));
    }

    @OnClick(R.id.bnt_alterview1)
    public void showAlterView1(View view) {
        AlertView alertView = new AlertView("标题", null, "取消", null,
                new String[]{"其他按钮1", "其他按钮2", "其他按钮3"}, this, AlertView.Style.ActionSheet, this).
                setCancelable(true);
        alertView.show();
    }


    @OnClick(R.id.btn_circleprogressview)
    public void someCustomView(View view) {
        startActivity(new Intent(UIActivity.this, CustomViewActivity.class));
    }

    @OnClick(R.id.bnt_alterview2)
    public void showAlterView2(View view) {
        BottomDialog.create(getSupportFragmentManager())
                .setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(View v) {
                        v.findViewById(R.id.btn_bottom_1).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Log.i(TAG, "onClick: " + ((Button) v).getText());
                            }
                        });
                    }

                })
                .setLayoutRes(R.layout.bottom_layout_ios)
                .setDimAmount(0.1f)
                .setCancelOutside(true)
                .setTag("bottomdialog")
                .show();
    }

    //滚轮选择 用recyclerview实现
    @OnClick(R.id.bnt_alterview3)
    public void showBottomDialog(View view) {
        BottomDialog.create(getSupportFragmentManager())
                .setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(View v) {
                        dealWith(v);
                    }
                })
                .setHeight(DensityUtil.dp2px(300))
                .setLayoutRes(R.layout.bottom_layout1)
                .setCancelOutside(true)
                .setDimAmount(0.4f)
                .setTag("bottomdialog")
                .show();
    }

    @OnClick(R.id.bnt_alterview4)
    public void showBottonDialog4(View view) {
        BottomDialog.create(getSupportFragmentManager())
                .setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(View v4) {
                        dealWithV4(v4);
                    }
                })
                .setHeight(DensityUtil.dp2px(300))
                .setLayoutRes(R.layout.bottom_layout4)
                .setCancelOutside(true)
                .setDimAmount(0.4f)
                .setTag("bottomdialog")
                .show();
    }

    private void dealWithV4(View v4) {
        RecyclerView recyclrviewYear = v4.findViewById(R.id.recyclerview_year);
        RecyclerView recyclrviewMonth = v4.findViewById(R.id.recyclerview_month);

        YearMonthModel yearMonthModel = new YearMonthModel();
        LinearLayoutManager year = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager month = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclrviewYear.setLayoutManager(year);
        recyclrviewMonth.setLayoutManager(month);

        SelectorAdapterYear yearAdapter = new SelectorAdapterYear(UIActivity.this, strings);
        SelectorAdapterMonth monthAdapter = new SelectorAdapterMonth(UIActivity.this, strings);
        recyclrviewYear.setAdapter(yearAdapter);
        recyclrviewMonth.setAdapter(monthAdapter);

        yearAdapter.setmYearListener(new SelectorAdapterYear.yearListener() {
            @Override
            public void onYeahClickListener(String year) {
                yearMonthModel.setYear(year);
            }
        });

        monthAdapter.setmMonthListener(new SelectorAdapterMonth.monthListener() {

            @Override
            public void onMonthClickListener(String month) {
                yearMonthModel.setMonth(month);
                Log.i(TAG, "onMonthClickListener: " + yearMonthModel.toString());
            }
        });

    }

    private void dealWith(View v) {
        RecyclerView recyclerview = v.findViewById(R.id.recyclerview_select);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerview.setLayoutManager(linearLayoutManager);
        SelectorAdapter selectorAdapter = new SelectorAdapter(UIActivity.this, strings);
        recyclerview.setAdapter(selectorAdapter);
    }

    @Override
    public void onItemClick(Object o, int position) {
        Log.i(TAG, "onItemClick: " + position);
    }

    //流式布局
    public void doFlowLayout(View view) {
        startActivity(new Intent(UIActivity.this, FlowLayoutActivity.class));
    }

    public void doProfile(View view) {
        startActivity(new Intent(UIActivity.this, MyProActivity.class));
    }

    public void doBanner(View view) {
        startActivity(new Intent(UIActivity.this,BannnerActivity.class));
    }

    public void doDanmu(View view) {
//        startActivity(new Intent(UIActivity.this,DanmuActivity.class));
    }
}
